package com.shinbada.modules.sys.entity;


import com.shinbada.common.utils.StringUtils;
import com.shinbada.core.persistence.DataEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 * 数据权限Entity
 *
 * @author initcap
 * @version 2017-04-02
 */
@Data
@ToString
@ApiModel("数据权限实体")
public class DataRule extends DataEntity<DataRule> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("所属菜单")
    private String menuId;
    @ApiModelProperty("数据规则名称")
    private String name;
    @ApiModelProperty("实体类名")
    private String className;
    @ApiModelProperty("规则字段")
    private String field;
    @ApiModelProperty("规则条件")
    private String express;
    @ApiModelProperty("规则值")
    private String value;
    @ApiModelProperty("自定义sql")
    private String sqlSegment;

    public DataRule() {
        super();
    }

    public DataRule(String id) {
        super(id);
    }

    public String getDataScopeSql() {
        StringBuffer sqlBuffer = new StringBuffer();
        if (StringUtils.isNotBlank(field) && StringUtils.isNotBlank(value)) {
            sqlBuffer.append(" AND " + field + " " + StringEscapeUtils.unescapeHtml4(express) + " " + value + " ");
        }
        if (StringUtils.isNotBlank(sqlSegment)) {
            sqlBuffer.append(" AND " + StringEscapeUtils.unescapeHtml4(sqlSegment) + " ");
        }

        return sqlBuffer.toString();
    }

}